import { Component, OnInit, Input } from '@angular/core';
import { NavParams, ModalController, Events, Platform, NavController } from '@ionic/angular';
import { ChangePhoneModal } from '../user-info/change-phone/change-phone';
import { DataService } from '@services/data.service';
import { CacheService } from '@services/cache.service';
import { LoginPage } from '../../pages/login/login';
import { GlobalService } from '@services/global.service';
import { JPush } from '@jiguang-ionic/jpush/ngx';
import { MyNoteService } from '@services/myNote.service';


@Component({
    selector: 'user-info',
    templateUrl: 'user-info.html',
    styleUrls: ['user-info.scss']
})
export class UserInfoModal implements OnInit {
    @Input() datas;
    infoName;
    infoIndex;
    modalNext;
    Password = {
        old: '',
        new: ''
    };
    password;
    userInfo = [
        { id: 0, name: '用户名' }, { id: 1, name: '密码' },
        { id: 2, name: '手机号' }, { id: 3, name: '邮箱' }
    ];
    userItems;
    constructor(public navParams: NavParams,
        public modalCtrl: ModalController,
        public events: Events,
        private dataService: DataService,
        private cache: CacheService,
        private myNote: MyNoteService,
        private jpush: JPush,
        private global: GlobalService,
        private navCtrl: NavController,
        private platform: Platform
    ) {
        // this.platform.backButton.subscribe(res => {
        //     this.closeModal();
        // })
        this.userItems = this.cache.userinfo;
        this.password = this.cache.upwd;
    }

    ngOnInit() {
        console.log(this.datas);
        this.password = this.cache.upwd;
        this.modalNext = this.datas.modalNext;
        const nameType: number = this.datas.index;
        for (let i = 0; i < this.userInfo.length; i++) {
            if (nameType - 2 === i) {
                this.infoName = this.userInfo[i].name;
                this.infoIndex = this.userInfo[i].id;
            }
        }
    }

    changeInfo(InfoType) {
        if (InfoType === 0) {
            if (!this.userItems.userName) {
                this.Alert('用户名不能为空');
            } else {
                this.dataService.UpadateName(this.userItems.userName, this.userItems.phone, '').subscribe(res => {
                    this.Alert('用户名修改成功');
                    this.cache.userinfo.userName = this.userItems.userName;
                    this.modalCtrl.dismiss({ InfoType: InfoType, value: this.userItems.userName });
                });
            }
        } else if (InfoType === 1) {
            if (!this.Password.old) {
                this.Alert('原密码不能为空');
            } else if (this.Password.old !== this.password) {
                this.Alert('原密码不正确');
            } else if (!this.Password.new) {
                this.Alert('新密码不能为空');
            } else if (!this.checkPwd(this.Password.new)) {
                this.Alert('新密码格式不对');
            } else {
                this.dataService.UpadatePassword(this.Password.new, this.Password.old).subscribe(res => {
                    if (res.success) {
                        this.cache.upwd = this.Password.new;
                        this.jpush.deleteAlias({
                            alias: '',
                            sequence: new Date().getTime()
                        });
                        this.global.isLogin = false;
                        this.cache.clearCache();
                        this.Alert('密码修改成功');
                        setTimeout(() => {
                            this.modalCtrl.dismiss({ InfoType: InfoType, value: this.userItems.userName });
                            this.navCtrl.navigateRoot('login');
                        }, 1000);
                    } else {
                        this.Alert(res.message);
                    }
                });
            }
        } else if (InfoType === 2) {
            console.log(this.userItems.Phone);
        } else if (InfoType === 3) {
            const reg = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
            if (!this.userItems.EMail) {
                this.Alert('邮箱不能为空');
            } else if (!reg.test(this.userItems.EMail)) {
                this.Alert('邮箱格式不正确');
            } else {
                this.Alert('邮箱修改成功');
                this.modalCtrl.dismiss({ InfoType: InfoType, value: this.userItems.EMail });
            }
        }
    }

    Alert(subTitle) {
        this.myNote.MyNotice(subTitle, 1500, null, 'alert-note');
    }

    checkPwd(pwd) {
        pwd = pwd.toLowerCase();
        const reg1 = /\d/;
        const reg2 = /[a-z]/;
        const reg3 = /[a-z0-9]{6,12}/;
        return (reg1.test(pwd) && reg2.test(pwd) && reg3.test(pwd));
    }

    async changePhone() {
        this.modalNext.present();
        this.modalCtrl.dismiss();
    }

    closeModal() {
        this.modalCtrl.dismiss();
    }
}
